package api

import (
	"Im/dto"
	"Im/global"
	"Im/service"
	"Im/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// @Tags User
// @Summary 登录
// @Description 用户登录的接口
// @Accept  json
// @Param data body dto.ImUserLoginDto true "登录参数data"
// @Router /login [post]
func ImUserLogin(c *gin.Context) {
	var userLogin dto.ImUserLoginDto
	err := c.ShouldBindJSON(&userLogin)
	if err != nil {
		return
	}
	err, data := service.ImUserLoginService(userLogin)
	token, _ := utils.GenerateToken(strconv.Itoa(data.ID), data.Email)
	if err != nil {
		Res(c, http.StatusOK, Response{
			Code: "200",
			Msg:  "登录成功",
			Data: gin.H{
				"token": token,
			},
		})
	}

}

func ImUserSignUp(c *gin.Context) {
	var userSignUp dto.ImUserSignDto
	err := c.ShouldBindJSON(&userSignUp)
	if err != nil {
		return
	}
	//调用service层的函数
	err = service.ImUserSignUp(userSignUp)
	if err != nil {
		Res(c, http.StatusInternalServerError, Response{
			Code: "500",
			Msg:  err.Error(),
		})
		return
	}

	Res(c, http.StatusOK, Response{
		Code: "200",
		Msg:  "账号注册成功",
	})
	delete(global.EmailCode, userSignUp.Email)
}
